﻿using SD.ContentSystem.Domain.Entities;
using SD.ContentSystem.Domain.IRepositories.Interfaces;
using SD.Infrastructure.Repository.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SD.ContentSystem.Repository.Implements
{
    /// <summary>
    /// 栏目仓储实现
    /// </summary>
    public class ChannelRepository : EFAggRootRepositoryProvider<Channel>, IChannelRepository
    {
        #region # 根据条件获取实体对象集合（默认排序） —— override IOrderedQueryable<Channel> FindBySort(...
        /// <summary>
        /// 根据条件获取实体对象集合（默认排序）
        /// </summary>
        /// <param name="condition">条件表达式</param>
        /// <returns>实体对象集合</returns>
        protected override IOrderedQueryable<Channel> FindBySort(Expression<Func<Channel, bool>> condition)
        {
            return base.Find(condition).OrderBy(x => x.Sort);
        }
        #endregion

        #region # 根据上级栏目获取栏目列表 —— IEnumerable<Channel> Find(string parentChannelNo...
        /// <summary>
        /// 根据上级栏目获取栏目列表
        /// </summary>
        /// <param name="parentChannelNo">上级栏目编号</param>
        /// <param name="isNavigator">是否是导航</param>
        /// <returns>栏目列表</returns>
        public IEnumerable<Channel> Find(string parentChannelNo, bool? isNavigator)
        {
            IQueryable<Channel> channels = this.FindAllInner();

            if (!string.IsNullOrWhiteSpace(parentChannelNo))
            {
                channels = channels.Where(x => x.ParentNode != null && x.ParentNode.Number == parentChannelNo);
            }
            if (isNavigator.HasValue)
            {
                channels = channels.Where(x => x.IsNavigator == isNavigator.Value);
            }

            return channels.OrderBy(x => x.Sort).AsEnumerable();
        }
        #endregion

        #region # 获取栏目列表 —— IEnumerable<Channel> Find(bool? isNavigator)
        /// <summary>
        /// 获取栏目列表
        /// </summary>
        /// <param name="isNavigator">是否是导航</param>
        /// <returns>栏目列表</returns>
        public IEnumerable<Channel> Find(bool? isNavigator)
        {
            Expression<Func<Channel, bool>> condition =
                x =>
                    (isNavigator == null || x.IsNavigator == isNavigator);

            return this.FindBySort(condition).AsEnumerable();
        }
        #endregion
    }
}
